package com.studynotes.dev.tech.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.studynotes.dev.tech.common.utils.OptionalUtil;
import com.studynotes.dev.tech.common.utils.QueryWrapperUtil;
import com.studynotes.dev.tech.mapper.DataSourceLinkConfigMapper;
import com.studynotes.dev.tech.model.dto.dataSourceLinkConfig.DataSourceLinkConfigCreate;
import com.studynotes.dev.tech.model.dto.dataSourceLinkConfig.DataSourceLinkConfigQuery;
import com.studynotes.dev.tech.model.dto.dataSourceLinkConfig.DataSourceLinkConfigUpdate;
import com.studynotes.dev.tech.model.entity.DataSourceLinkConfig;
import com.studynotes.dev.tech.model.vo.dataSourceLinkConfig.DataSourceLinkConfigView;
import com.studynotes.dev.tech.service.DataSourceLinkConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Liu XiaoJun
 * @description: 数据源链接配置 业务服务实现
 * @date 2025-11-07 11:12:53
 */
@Service
public class DataSourceLinkConfigServiceImpl implements DataSourceLinkConfigService {

    @Autowired
    private DataSourceLinkConfigMapper dataSourceLinkConfigMapper;

    @Override
    public DataSourceLinkConfigView create(DataSourceLinkConfigCreate create) {
        DataSourceLinkConfig entity = new DataSourceLinkConfig();
        BeanUtil.copyProperties(create,entity);
        dataSourceLinkConfigMapper.insert(entity);
        return BeanUtil.copyProperties(entity,DataSourceLinkConfigView.class);
    }

    @Override
    public boolean delete(String dbLinkKey) {
        DataSourceLinkConfig entity = dataSourceLinkConfigMapper.selectById(dbLinkKey);
        OptionalUtil.isNotNullWithout(entity, () -> {
            entity.setFlag("2");
            dataSourceLinkConfigMapper.updateById(entity);
        });
        return true;
    }

    @Override
    public DataSourceLinkConfigView update(String dbLinkKey, DataSourceLinkConfigUpdate update) {
        DataSourceLinkConfig entity = dataSourceLinkConfigMapper.selectById(dbLinkKey);
        OptionalUtil.isNotNullWithout(entity, () -> {
            BeanUtil.copyProperties(update,entity);
            dataSourceLinkConfigMapper.updateById(entity);
        });
        return BeanUtil.copyProperties(entity,DataSourceLinkConfigView.class);
    }

    @Override
    public DataSourceLinkConfigView get(String dbLinkKey) {
        DataSourceLinkConfig entity = dataSourceLinkConfigMapper.selectById(dbLinkKey);
        return BeanUtil.copyProperties(entity,DataSourceLinkConfigView.class);
    }

    @Override
    public List<DataSourceLinkConfigView> queryList(DataSourceLinkConfigQuery data) {
        QueryWrapper<DataSourceLinkConfig> queryWrapper = QueryWrapperUtil.queryToWrapper(data, DataSourceLinkConfig.class);
        List<DataSourceLinkConfig> list = dataSourceLinkConfigMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(list,DataSourceLinkConfigView.class);
    }
}
